package com.carpool.finder;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class ImageFinder {

	public Bitmap findImage(String image_url){
		
		try {
		       URL url = new URL(image_url);
		       HttpGet httpRequest = null;

		       try {
				httpRequest = new HttpGet(url.toURI());
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		       HttpClient httpclient = new DefaultHttpClient();
		       HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

		       HttpEntity entity = response.getEntity();
		       BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
		       InputStream input = b_entity.getContent();

		       Bitmap bitmap = BitmapFactory.decodeStream(input);
		       
		       return bitmap; 

		    } catch (MalformedURLException e) {
		        Log.e("log", "bad url"+e);
		    } catch (IOException e) {
		        Log.e("log", "io error"+e);
		    }
		return null;
		
	}
}
